package com.taotao.manage.service;

import java.lang.reflect.ParameterizedType;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.github.abel533.entity.Example;
import com.github.abel533.entity.Example.Criteria;
import com.github.abel533.mapper.Mapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.manage.pojo.BasePojo;

@SuppressWarnings("unchecked")
public abstract class BaseService<T extends BasePojo> {

	@Autowired
	private Mapper<T> mapper;
//	public abstract Mapper<T> getMapper;

	public Class<T> clazz;

	{
		ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
		this.clazz = (Class<T>) type.getActualTypeArguments()[0];
	}

	/**
	 * 根据主键查询信息
	 * 
	 * @param id
	 * @return
	 */
	public T queryById(Long id) {
		return this.mapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询所有记录
	 * 
	 * @return
	 */
	public List<T> queryAll() {
		return this.mapper.select(null);
	}

	/**
	 * 查询一条记录
	 * 
	 * @param record
	 * @return
	 */
	public T queryOne(T record) {
		return this.mapper.selectOne(record);
	}

	/**
	 * 根据条件查询信息
	 * 
	 * @return
	 */
	public List<T> queryListByWhere(T record) {
		return this.mapper.select(record);
	}

	/**
	 * 根据条件分页查询
	 * 
	 * @param record
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public PageInfo<T> queryPageListByWhere(T record, Integer pageNum, Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<T> list = this.mapper.select(record);
		return new PageInfo<>(list);
	}
	
	/**
	 * 分页查询并排序
	 * 
	 * @param record
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public PageInfo<T> queryPageListByOrderBy(String orderByClause, Integer pageNum, Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		Example example = new Example(clazz);
		example.setOrderByClause(orderByClause);
		List<T> list = this.mapper.selectByExample(example);
		return new PageInfo<>(list);
	}

	/**
	 * 新增数据
	 * 
	 * @param record
	 * @return
	 */
	public Integer save(T record) {
		record.setCreated(new Date());
		record.setUpdated(record.getCreated());
		return this.mapper.insert(record);
	}

	/**
	 * 新增数据，新增不为null的字段
	 * 
	 * @param record
	 * @return
	 */
	public Integer saveSelective(T record) {
		record.setCreated(new Date());
		record.setUpdated(record.getCreated());
		return this.mapper.insertSelective(record);
	}

	/**
	 * 更新
	 * 
	 * @param record
	 * @return
	 */
	public Integer update(T record) {
		record.setUpdated(new Date());
		return this.mapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新，不为null的字段
	 * 
	 * @param record
	 * @return
	 */
	public Integer updateSelective(T record) {
		record.setUpdated(new Date());
		record.setCreated(null);
		return this.mapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 根据id删除
	 * 
	 * @param id
	 * @return
	 */
	public Integer deleteById(Long id) {
		return this.mapper.deleteByPrimaryKey(id);
	}

	/**
	 * 根据多个id删除用户信息
	 * 
	 * @param ids
	 * @param clazz
	 * @param property
	 * @return
	 */
	public Integer deleteByIds(List<Object> ids, String property) {
		Example example = new Example(clazz);
		Criteria criteria = example.createCriteria();
		criteria.andIn(property, ids);
		return this.mapper.deleteByExample(example);
	}

	/**
	 * 根据条件删除记录
	 * 
	 * @return
	 */
	public Integer deleteByWhere(T record) {
		return this.mapper.delete(record);
	}
}
